package com.jboost.modules.app.service.strategry.pdm;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.jboost.common.util.HttpRestUtils;
import com.jboost.common.util.JSONUtil;
import com.jboost.modules.app.constant.BaseResult;
import com.jboost.modules.app.dto.JdyReq;
import com.jboost.modules.app.dto.PdmBanDanDetailReqDto;
import com.jboost.modules.app.dto.PdmMl;
import com.jboost.modules.app.dto.resp.BDCB;
import com.jboost.modules.app.dto.resp.BdDy;
import com.jboost.modules.app.dto.resp.BdSJ;
import com.jboost.modules.app.service.strategry.common.AbstractJdy;
import com.jboost.modules.system.entity.PdmBom;
import com.jboost.modules.system.mapper.PdmBomMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.ApplicationRunner;
import org.springframework.http.HttpHeaders;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.*;
import java.util.concurrent.CompletableFuture;

//@Service(value = "PdmBanDanDetailQuery")
@Slf4j
public class PdmBanDanDetailQuery extends AbstractJdy<String, PdmBom, PdmBomMapper, PdmBom>{


    public String getFormId() {
        return "651fbb02391f2adff5ba5fd4";
    }

    public HttpHeaders getJdyHeaders(){
        HttpHeaders jdyHeaders = new HttpHeaders();
        jdyHeaders.set("Authorization","Bearer "+ "j2tq9lljcelsqTPZnbr36USxr6yZWsHu");
        return jdyHeaders;
    }
    //64e1f2d0d8b6100009e88053

    public String getAppId() {
        return "64e1f2d0d8b6100009e88053";
    }




    @Scheduled(initialDelay = 1000, fixedRate = 60000)
    public BaseResult<Boolean> invokeStrategy() throws Exception {
        try {
            sync(new PdmBom());
            return BaseResult.success();
        }catch (Exception exception){

            System.out.println(1);

        }
        return BaseResult.success();
    }

    public void sync(PdmBom entit) throws IOException {
        log.info("开始批量查询Hs材料数据");
        HashMap<String, Object> param = getParam();
        param.put("limit",100);
        List<JSONObject> jsonObjects = new ArrayList<>();
        do {
            String post = HttpRestUtils.post(query_all_api, param, getJdyHeaders());
            jsonObjects   = (List<JSONObject>) JSONUtil.fromJson(post, Map.class).get("data");
            jsonObjects.forEach(e1 -> {
                String uuid = e1.getString("_widget_1695299408498");
                String status = e1.getString("_widget_1695300314310");
                if (Objects.equals(status,"正常")) {
                    JdyReq jdyReq = new JdyReq();
                    jdyReq.setApp_id(getAppId());
                    jdyReq.setEntry_id(getFormId());
                    jdyReq.setLimit(100);
                    jdyReq.setFields(Arrays.asList("_widget_1695299408498", "_widget_1695300314310", "_id"));
                    JdyReq.FilterBean filterBean = new JdyReq.FilterBean();
                    filterBean.setRel("and");
                    List<JdyReq.FilterBean.CondBean> cond = new ArrayList<>();
                    JdyReq.FilterBean.CondBean a = new JdyReq.FilterBean.CondBean();
                    a.setField("_widget_1695299408498");
                    a.setType("text");
                    a.setMethod("eq");
                    a.setValue(Arrays.asList(uuid));
                    cond.add(a);

                    JdyReq.FilterBean.CondBean a1 = new JdyReq.FilterBean.CondBean();
                    a1.setField("_widget_1695300314310");
                    a1.setType("text");
                    a1.setMethod("eq");
                    a1.setValue(Arrays.asList("正常"));
                    cond.add(a1);
                    filterBean.setCond(cond);
                    jdyReq.setFilter(filterBean);
                    try {
                        String s = jdyQueryAll(jdyReq);
                        JSONArray jsonArray = (JSONArray) JSONUtil.fromJson(s, HashMap.class).get("data");
                        if (jsonArray.size() > 1) {
                            for (int i = 0; i < jsonArray.size()-1; i++) {
                                JSONObject jsonObject = (JSONObject) jsonArray.get(i);
                                String dataId = (String) jsonObject.get("_id");
                                PdmBanDanDetailReqDto pdmBanDanDetailReqDto = new PdmBanDanDetailReqDto();
                                pdmBanDanDetailReqDto.set_widget_1695300314310(getParaMap("已取消"));
                                jdyUpdate(dataId,pdmBanDanDetailReqDto);
                            }
                        }
                    } catch (IOException ex) {

                    }
                }

            });
            if (jsonObjects.size()>0){

                param.put("data_id",jsonObjects.get(jsonObjects.size()-1).getString("_id"));
            }
        }while (jsonObjects.size()>0);

    }



}
